package com.epigeon.amadeo.test;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ArrayAdapter;

import com.epigeon.amadeo.impl.PassiveList;
import com.epigeon.amadeo.impl.XMPPConnectionInput;
import com.epigeon.amadeo.model.Message;
import com.epigeon.amadeo.model.XMPPMessage;

public class PassiveMessageList extends PassiveList {

	private List<String> messageList;
	private ArrayAdapter<String> adapter;
	
	public PassiveMessageList(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.messageList = new LinkedList<String>();
		this.adapter = new ArrayAdapter<String>(this.getContext(), R.layout.multi_line_list_item, this.messageList);
		this.setAdapter(adapter);
	}
	
	@Override
	public void putMessage(Message message) {
		if ( XMPPConnectionInput.MESSAGE_TYPE_RECEIVE.equals( message.getType() ) ||  XMPPConnectionInput.MESSAGE_TYPE_SEND.equals( message.getType() ) ) {
			XMPPMessage xMessage = (XMPPMessage) message;
			messageList.add(xMessage.getFrom()+ " : " + xMessage.getBody());
			Log.d("hehe", xMessage.getFrom() + " : " + xMessage.getBody());
			this.post(new Runnable() {
				public void run() {
					refreshData();					
				}			
			});
		}		
	}
	
	public List<String> getMessageList() {
		return messageList;
	}
		
	private void refreshData() {
		this.adapter = new ArrayAdapter<String>(this.getContext(), R.layout.multi_line_list_item, this.messageList);
		this.setAdapter(adapter);
		this.setSelection(adapter.getCount() - 1);
	}
}
